// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Rescue x amount of civils
// -----------------------------------------------

private [
	"_unit",
	"_missioncomplete",
	"_position",
	"_civils",
	"_civil",
	"_group",
	"_type",
	"_count",
	"_number",
	"_counter"
];

_unit   = _this select 0;
_number = _this select 1;

_civils = [];

_missioncomplete = false;

//A3 TODO: setVehicleInit
_group = createGroup civilian;
for "_i" from 1 to _number do {
	_type = wcrescuecivils call BIS_fnc_selectRandom;
	_civil = _group createUnit [_type, position _unit, [], 5, "FORM"];
	_civil setVehicleInit "this addAction ['<t color=''#ffcb00''>Hands up</t>', 'warcontext\actions\WC_fnc_dohandsup.sqf', [], 6, false, true];";
	_civil setVehicleInit "this addAction ['<t color=''#ffcb00''>Follow me</t>', 'warcontext\actions\WC_fnc_dofollowme.sqf', [], 6, false, true];";
	_civil setVehicleInit "this addAction ['<t color=''#ffcb00''>Search Civil</t>', 'warcontext\actions\WC_fnc_dosearchsomeone.sqf', [], 6, false, true];";
	processInitCommands;
	_civil stop true;
	sleep 0.5;
};

_civils = units _group;
_group allowFleeing 0;

wcgarbage = [_group] spawn WC_fnc_civilhandler;	

_counter = 20;
while {!_missioncomplete} do {
	_count = 0;
	{
		if (_x distance getMarkerPos "respawn_west" < 100) then {
			_x doMove getMarkerPos "respawn_west";
			_count = _count + 1;
		};
		if !(alive _x) then {
			_civils = _civils - [_x];
		};
	} foreach _civils;

	if (_count == count _civils) then {
		wcmessageW = [localize "STR_WC_MESSAGEMISSIONCOMPLETED", localize "STR_WC_MESSAGELEAVEZONE"];
		["wcmessageW", "client"] call WC_fnc_publicvariable;
		wcmissionsuccess = true;
		_missioncomplete = true;
		wcleveltoadd = 1;
		wcfame = wcfame + wcbonusfame;
		wcenemyglobalelectrical = wcenemyglobalelectrical + wcbonuselectrical;
		wcenemyglobalfuel = wcenemyglobalfuel + wcbonusfuel;
	};

	if (count _civils < floor(0.80 * _number)) then {
		wcmessageW = [localize "STR_WC_MESSAGEMISSIONFAILED", localize "STR_WC_MESSAGELEAVEZONE"];
		["wcmessageW", "client"] call WC_fnc_publicvariable;
		wcmissionsuccess = true;
		_missioncomplete = true;
		wcleveltoadd = 0;
	};

	if (_counter > 20) then {
		_position = (position (leader _group)) findEmptyPosition [1, 20];
		"SmokeShellRed" createVehicle _position;
		wcmessageW = [format ["Still %1 civils", (count _civils - _count)], "to rescue"];
		["wcmessageW", "client"] call WC_fnc_publicvariable;
		_counter = 0;
	};
	_counter = _counter + 1;
	sleep 5;
};